<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <head>
        <link rel="stylesheet" href="index.css">
    </head>
</head>
<body>
<div class="keys">
    <div class="key" data-key="65">
        <kbd>A</kbd>
        <span class="sound">clap</span>
    </div>
    <div class="key" data-key="83">
        <kbd>S</kbd>
        <span class="sound">hihat</span>
    </div>
    <div class="key" data-key="68">
        <kbd>D</kbd>
        <span class="sound">kick</span>
    </div>
    <div class="key" data-key="70">
        <kbd>F</kbd>
        <span class="sound">openhat</span>
    </div>
    <div class="key" data-key="71">
        <kbd>G</kbd>
        <span class="sound">boom</span>
    </div>
    <div class="key" data-key="74">
        <kbd>J</kbd>
        <span class="sound">snare</span>
    </div>
    <div class="key" data-key="75">
        <kbd>K</kbd>
        <span class="sound">tom</span>
    </div>
    <div class="key" data-key="76">
        <kbd>L</kbd>
        <span class="sound">tink</span>
    </div>
</div>
<audio data-key="65" src="sounds/clap.wav"></audio>
<audio data-key="83" src="sounds/hihat.wav"></audio>
<audio data-key="68" src="sounds/kick.wav"></audio>
<audio data-key="70" src="sounds/boom.wav"></audio>
<audio data-key="71" src="sounds/ride.wav"></audio>
<audio data-key="74" src="sounds/snare.wav"></audio>
<audio data-key="75" src="sounds/tom.wav"></audio>
<audio data-key="76" src="sounds/tink.wav"></audio>

<script>
    function playSound(e) {

        const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
        const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
        if (!audio) return
        audio.currentTime = 0;
        audio.play()
        key.classList.add('playing');


    }

    function removeTransition(e) {
        if (e.propertyName !== 'transform') return;
        this.classList.remove('playing');

    }

    const keys = document.querySelectorAll('.key');
    keys.forEach(key => key.addEventListener('transitionend', removeTransition))
    window.addEventListener('keydown', playSound)

</script>
</body>
</html>
